package com.homegoods.login;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.support.annotation.IntDef;
import android.widget.TextView;

import com.kedll.library.util.StoreShareValue;
import com.kedll.library.util.ToastUtil;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;

/**
 * 获取短信验证码工具类
 *
 * @author QiQi
 * @date 15/10/28
 */
public class KGetVerHelper {
    /**
     * 初始状态
     */
    public static final int NONE = 593;
    /**
     * 验证码发送成功，倒计时状态
     */
    public static final int COUNTDOWN = 60;
    public final static String TAG = KGetVerHelper.class.getName();
    public static final String REQ = "reg";//注册
    public static final String LOGIN = "sign";// 登录
    public static final String FPW = "pword";//找回密码
    private final int IS_USER_EXIST = 0X4001;
    private int count = 60;
    private TextView mVerView;
    private Context mContext;
    private int mState = NONE;
    private CountDownTimer mTimer;

    /**
     * 构造方法
     *
     * @param ctx     上下文
     * @param verView 获取验证码的按钮
     */
    public KGetVerHelper(Context ctx, TextView verView) {
        super();
        this.mVerView = verView;
        this.mContext = ctx;

    }

    /**
     * 发送成功
     */
    public void sendSuc() {
        ToastUtil.showToast(R.string.string_ver_code_has_send);
        getVerification();
    }

    /**
     * 请求短信验证码成功倒计时
     */
    public void getVerification() {
        resetCount();
        start();
        mVerView.setEnabled(false);
    }

    /**
     * 重置获取验证码按钮状态
     */
    public void resetCount() {
        count = 60;
        mVerView.setText(R.string.string_get_ver);
        mVerView.setEnabled(true);
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        mState = NONE;
    }

    /**
     * 开始倒计时
     */
    public void start() {
        if (mTimer == null) {
            mTimer = new MyCountDownTimer(count, this);
        }
        mTimer.start();
        mState = COUNTDOWN;
    }

    /**
     * 停止倒计时
     */
    public void stop() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (mState == COUNTDOWN) {
            StoreShareValue.putLong("countDownTime", SystemClock.elapsedRealtime(), mContext, StoreShareValue.SAVE_KEY_TOKEN);
        }
    }

    /**
     * 重新启动获取验证码倒计时
     */
    public void restart() {
        if (mState == COUNTDOWN) {
            int count = getCount();
            if (count > 0) {
                int offset = (int) ((SystemClock.elapsedRealtime() - StoreShareValue.getLong("countDownTime", System.currentTimeMillis(), mContext, StoreShareValue.SAVE_KEY_TOKEN)) / 1000);
                if (offset < count && offset > 0) {
                    setCount(count - offset);
                    start();
                } else {
                    resetCount();
                }
            }
        }
    }

    /**
     * 获取当前倒计时
     *
     * @return
     */
    private int getCount() {
        return count;
    }

    private void setCount(int count) {
        this.count = count;
    }

    public void cancel() {
        mVerView.setOnClickListener(null);
        mVerView = null;
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        StoreShareValue.putLong("countDownTime", 0L, mContext, StoreShareValue.SAVE_KEY_TOKEN);
    }

    @IntDef({
            NONE,
            COUNTDOWN})
    @Retention(RetentionPolicy.SOURCE)
    public @interface STATE {
    }

    private final static class MyCountDownTimer extends CountDownTimer {
        private WeakReference<KGetVerHelper> reference;

        MyCountDownTimer(long millisInFuture, KGetVerHelper helper) {
            super(millisInFuture * 1000, 1000);
            reference = new WeakReference<>(helper);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            KGetVerHelper helper = reference.get();
            if (helper != null) {
                String text = String.format(helper.mContext.getString(R.string.string_time_to_send_ver), millisUntilFinished / 1000);
                helper.mVerView.setText(text);
            }
        }

        @Override
        public void onFinish() {
            KGetVerHelper helper = reference.get();
            if (helper != null) {
                helper.resetCount();
            }
        }
    }

}
